package com.bdth.service.impl;

import com.bdth.mapper.DepartmentMapper;
import com.bdth.mapper.UserMapper;
import com.bdth.model.Department;
import com.bdth.model.User;
import com.bdth.model.UserDepartmentKey;
import com.bdth.service.IUserDepService;
import com.bdth.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

	@Autowired
	private UserMapper mapper;

	@Autowired
	private DepartmentMapper departmentMapper;

	@Autowired
	private IUserDepService userDepService;

	public User queryLogin(String loginName, String passWord) {
		User model = new User();
		model.setLoginName(loginName);
		model.setPassWord(passWord);
		return mapper.queryLogin(model);
	}


	//@Cacheable("queryMapDataByMapList")
	public List<Map> queryMapDataByMapList(Map map) throws Exception {

		return mapper.queryMapDataByMapList(map);
	}


	public User queryUserByLoginName(String loginName) {
		return mapper.queryUserByLoginName(loginName);
	}

	@Override
	public void add(User user, Integer depId) throws Exception {
		super.add(user);
		addUserDep(user.getId(), depId);
	}


	/**
	 * 添加用户&分组
	 */
	public void addUserDep(Integer userId, Integer depId)
			throws Exception {

		UserDepartmentKey ud = new UserDepartmentKey();
		ud.setUserID(userId);
		ud.setDepID(depId);
		userDepService.add(ud);

	}


	public void update(User user, Integer depId)
			throws Exception {
		super.update(user);
		//清除关联关系
		userDepService.deleteByUserId(user.getId());
		addUserDep(user.getId(), depId);

	}

	@Override
	public Department queryDepByUserId(Integer userId) {
		return departmentMapper.queryDepByUserId(userId);
	}
}
